package com.example.testlxr9096.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.testlxr9096.R;
import com.example.testlxr9096.bean.TestRecord;

import java.util.List;

public class TestRecordAdapter extends RecyclerView.Adapter<TestRecordAdapter.ViewHolder> {
    private List<TestRecord> testRecords;

    public TestRecordAdapter(List<TestRecord> testRecords) {
        this.testRecords = testRecords;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_test_record, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        TestRecord testRecord = testRecords.get(position);
        holder.bind(testRecord);
    }

    @Override
    public int getItemCount() {
        return testRecords.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tvId, tvDate, tvType, tvName, tvErrorInfo, tvState;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvId = itemView.findViewById(R.id.tv_id);
            tvDate = itemView.findViewById(R.id.tv_date);
            tvType = itemView.findViewById(R.id.tv_type);
            tvName = itemView.findViewById(R.id.tv_name);
            tvErrorInfo = itemView.findViewById(R.id.tv_error_info);
            tvState = itemView.findViewById(R.id.tv_state);
        }

        public void bind(TestRecord testRecord) {
            tvId.setText(String.valueOf(testRecord.getId()));
            tvDate.setText(testRecord.getDate());
            tvType.setText(testRecord.getType());
            tvName.setText(testRecord.getName());
            tvErrorInfo.setText(testRecord.getErrorInfo());
            tvState.setText(testRecord.getState());
        }
    }
}
